<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>2. Kubernetes详细教程-kubernetes集群环境搭建 | 小鹏杂谈</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/favicon.ico">
    <meta name="description" content="小鹏杂谈">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/assets/css/0.styles.8516fda1.css" as="style"><link rel="preload" href="/assets/js/app.b9ccfc55.js" as="script"><link rel="preload" href="/assets/js/4.8390fbcb.js" as="script"><link rel="preload" href="/assets/js/1.3334f995.js" as="script"><link rel="preload" href="/assets/js/28.57336847.js" as="script"><link rel="prefetch" href="/assets/js/10.8eb4e356.js"><link rel="prefetch" href="/assets/js/11.31befa4c.js"><link rel="prefetch" href="/assets/js/12.da37eb75.js"><link rel="prefetch" href="/assets/js/13.b24ba73c.js"><link rel="prefetch" href="/assets/js/14.3bbed283.js"><link rel="prefetch" href="/assets/js/15.fac60f8d.js"><link rel="prefetch" href="/assets/js/16.b75d2036.js"><link rel="prefetch" href="/assets/js/17.116317c5.js"><link rel="prefetch" href="/assets/js/18.80fdbeda.js"><link rel="prefetch" href="/assets/js/19.459fc46f.js"><link rel="prefetch" href="/assets/js/20.452f30f9.js"><link rel="prefetch" href="/assets/js/21.99f641dc.js"><link rel="prefetch" href="/assets/js/22.081dc33b.js"><link rel="prefetch" href="/assets/js/23.d3455a59.js"><link rel="prefetch" href="/assets/js/24.8f5d39e0.js"><link rel="prefetch" href="/assets/js/25.c3dc47fb.js"><link rel="prefetch" href="/assets/js/26.d93b5cf2.js"><link rel="prefetch" href="/assets/js/27.6d2089a5.js"><link rel="prefetch" href="/assets/js/29.903b5e06.js"><link rel="prefetch" href="/assets/js/3.32100170.js"><link rel="prefetch" href="/assets/js/30.274666a3.js"><link rel="prefetch" href="/assets/js/31.fa9bd0f8.js"><link rel="prefetch" href="/assets/js/32.20179720.js"><link rel="prefetch" href="/assets/js/33.2dcc611d.js"><link rel="prefetch" href="/assets/js/34.ba79122c.js"><link rel="prefetch" href="/assets/js/35.bcd9298f.js"><link rel="prefetch" href="/assets/js/36.bf6245d9.js"><link rel="prefetch" href="/assets/js/37.734eb893.js"><link rel="prefetch" href="/assets/js/38.26999070.js"><link rel="prefetch" href="/assets/js/39.cc1f016d.js"><link rel="prefetch" href="/assets/js/40.da84c263.js"><link rel="prefetch" href="/assets/js/41.a96b8efa.js"><link rel="prefetch" href="/assets/js/42.d586cccb.js"><link rel="prefetch" href="/assets/js/43.69bb61aa.js"><link rel="prefetch" href="/assets/js/44.3be5a840.js"><link rel="prefetch" href="/assets/js/45.208a3bc3.js"><link rel="prefetch" href="/assets/js/46.2d6cacad.js"><link rel="prefetch" href="/assets/js/47.5add378a.js"><link rel="prefetch" href="/assets/js/48.587139cc.js"><link rel="prefetch" href="/assets/js/49.57f7c25a.js"><link rel="prefetch" href="/assets/js/5.ff1e84d1.js"><link rel="prefetch" href="/assets/js/50.efde6e4a.js"><link rel="prefetch" href="/assets/js/51.c9447e5f.js"><link rel="prefetch" href="/assets/js/52.57a51c35.js"><link rel="prefetch" href="/assets/js/53.4be0f240.js"><link rel="prefetch" href="/assets/js/54.8dabd349.js"><link rel="prefetch" href="/assets/js/55.3b8ae0e4.js"><link rel="prefetch" href="/assets/js/56.07e830b2.js"><link rel="prefetch" href="/assets/js/57.8c0dbdcc.js"><link rel="prefetch" href="/assets/js/58.63387df6.js"><link rel="prefetch" href="/assets/js/59.959bb8c8.js"><link rel="prefetch" href="/assets/js/6.56a2b5cd.js"><link rel="prefetch" href="/assets/js/60.1041d86f.js"><link rel="prefetch" href="/assets/js/61.f58e6c18.js"><link rel="prefetch" href="/assets/js/62.a1a460c7.js"><link rel="prefetch" href="/assets/js/63.6c611c7d.js"><link rel="prefetch" href="/assets/js/64.6711119c.js"><link rel="prefetch" href="/assets/js/65.4655c3b1.js"><link rel="prefetch" href="/assets/js/66.e5dfe864.js"><link rel="prefetch" href="/assets/js/67.37515de0.js"><link rel="prefetch" href="/assets/js/68.0c8ba92d.js"><link rel="prefetch" href="/assets/js/69.52121d38.js"><link rel="prefetch" href="/assets/js/7.6f143d9f.js"><link rel="prefetch" href="/assets/js/70.1ab4d6a1.js"><link rel="prefetch" href="/assets/js/71.dc99dd1d.js"><link rel="prefetch" href="/assets/js/72.02e21e52.js"><link rel="prefetch" href="/assets/js/73.21efc121.js"><link rel="prefetch" href="/assets/js/74.649f4e1d.js"><link rel="prefetch" href="/assets/js/75.331c13e7.js"><link rel="prefetch" href="/assets/js/76.a94b5ff0.js"><link rel="prefetch" href="/assets/js/8.d4d40f2a.js"><link rel="prefetch" href="/assets/js/9.1b8824e7.js">
    <link rel="stylesheet" href="/assets/css/0.styles.8516fda1.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar" data-v-1156296a><div data-v-1156296a><div id="loader-wrapper" class="loading-wrapper" data-v-d48f4d20 data-v-1156296a data-v-1156296a><div class="loader-main" data-v-d48f4d20><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-4e82dffc data-v-1156296a data-v-1156296a><h3 class="title" data-v-4e82dffc data-v-4e82dffc>小鹏杂谈</h3> <p class="description" data-v-4e82dffc data-v-4e82dffc>小鹏杂谈</p> <label id="box" class="inputBox" data-v-4e82dffc data-v-4e82dffc><input type="password" value="" data-v-4e82dffc> <span data-v-4e82dffc>Konck! Knock!</span> <button data-v-4e82dffc>OK</button></label> <div class="footer" data-v-4e82dffc data-v-4e82dffc><span data-v-4e82dffc><i class="iconfont reco-theme" data-v-4e82dffc></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-4e82dffc>vuePress-theme-reco</a></span> <span data-v-4e82dffc><i class="iconfont reco-copyright" data-v-4e82dffc></i> <a data-v-4e82dffc><span data-v-4e82dffc>lzpeng723</span>
            
          <span data-v-4e82dffc>2021 - </span>
          2022
        </a></span></div></div> <div class="hide" data-v-1156296a><header class="navbar" data-v-1156296a><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/logo.png" alt="小鹏杂谈" class="logo"> <span class="site-name">小鹏杂谈</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/运维/" class="nav-link"><i class="undefined"></i>
  运维
</a></li><li class="dropdown-item"><!----> <a href="/categories/开发/" class="nav-link"><i class="undefined"></i>
  开发
</a></li><li class="dropdown-item"><!----> <a href="/categories/后端/" class="nav-link"><i class="undefined"></i>
  后端
</a></li><li class="dropdown-item"><!----> <a href="/categories/前端/" class="nav-link"><i class="undefined"></i>
  前端
</a></li><li class="dropdown-item"><!----> <a href="/categories/软件/" class="nav-link"><i class="undefined"></i>
  软件
</a></li><li class="dropdown-item"><!----> <a href="/categories/文档/" class="nav-link"><i class="undefined"></i>
  文档
</a></li><li class="dropdown-item"><!----> <a href="/categories/数据库/" class="nav-link"><i class="undefined"></i>
  数据库
</a></li><li class="dropdown-item"><!----> <a href="/categories/Node.js/" class="nav-link"><i class="undefined"></i>
  Node.js
</a></li><li class="dropdown-item"><!----> <a href="/categories/游戏/" class="nav-link"><i class="undefined"></i>
  游戏
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      文档
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/docs/minimal-boot/" class="nav-link"><i class="undefined"></i>
  minimal-boot
</a></li><li class="dropdown-item"><!----> <a href="/docs/minimal-cloud/" class="nav-link"><i class="undefined"></i>
  minimal-cloud
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      联系方式
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/lzpeng723" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/lzpeng723" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-mayun"></i>
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://www.zhihu.com/people/lzpeng723" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-zhihu"></i>
  知乎
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-1156296a></div> <aside class="sidebar" data-v-1156296a><div class="personal-info-wrapper" data-v-828910c6 data-v-1156296a><img src="/avatar.jpg" alt="author-avatar" class="personal-img" data-v-828910c6> <h3 class="name" data-v-828910c6>
    lzpeng723
  </h3> <div class="num" data-v-828910c6><div data-v-828910c6><h3 data-v-828910c6>66</h3> <h6 data-v-828910c6>Articles</h6></div> <div data-v-828910c6><h3 data-v-828910c6>19</h3> <h6 data-v-828910c6>Tags</h6></div></div> <ul class="social-links" data-v-828910c6></ul> <hr data-v-828910c6></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/运维/" class="nav-link"><i class="undefined"></i>
  运维
</a></li><li class="dropdown-item"><!----> <a href="/categories/开发/" class="nav-link"><i class="undefined"></i>
  开发
</a></li><li class="dropdown-item"><!----> <a href="/categories/后端/" class="nav-link"><i class="undefined"></i>
  后端
</a></li><li class="dropdown-item"><!----> <a href="/categories/前端/" class="nav-link"><i class="undefined"></i>
  前端
</a></li><li class="dropdown-item"><!----> <a href="/categories/软件/" class="nav-link"><i class="undefined"></i>
  软件
</a></li><li class="dropdown-item"><!----> <a href="/categories/文档/" class="nav-link"><i class="undefined"></i>
  文档
</a></li><li class="dropdown-item"><!----> <a href="/categories/数据库/" class="nav-link"><i class="undefined"></i>
  数据库
</a></li><li class="dropdown-item"><!----> <a href="/categories/Node.js/" class="nav-link"><i class="undefined"></i>
  Node.js
</a></li><li class="dropdown-item"><!----> <a href="/categories/游戏/" class="nav-link"><i class="undefined"></i>
  游戏
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      文档
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/docs/minimal-boot/" class="nav-link"><i class="undefined"></i>
  minimal-boot
</a></li><li class="dropdown-item"><!----> <a href="/docs/minimal-cloud/" class="nav-link"><i class="undefined"></i>
  minimal-cloud
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      联系方式
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/lzpeng723" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/lzpeng723" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-mayun"></i>
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://www.zhihu.com/people/lzpeng723" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-zhihu"></i>
  知乎
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav> <!----> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-4e82dffc data-v-1156296a><h3 class="title" data-v-4e82dffc data-v-4e82dffc>2. Kubernetes详细教程-kubernetes集群环境搭建</h3> <!----> <label id="box" class="inputBox" data-v-4e82dffc data-v-4e82dffc><input type="password" value="" data-v-4e82dffc> <span data-v-4e82dffc>Konck! Knock!</span> <button data-v-4e82dffc>OK</button></label> <div class="footer" data-v-4e82dffc data-v-4e82dffc><span data-v-4e82dffc><i class="iconfont reco-theme" data-v-4e82dffc></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-4e82dffc>vuePress-theme-reco</a></span> <span data-v-4e82dffc><i class="iconfont reco-copyright" data-v-4e82dffc></i> <a data-v-4e82dffc><span data-v-4e82dffc>lzpeng723</span>
            
          <span data-v-4e82dffc>2021 - </span>
          2022
        </a></span></div></div> <div data-v-1156296a><main class="page"><section><div class="page-title"><h1 class="title">2. Kubernetes详细教程-kubernetes集群环境搭建</h1> <div data-v-1ff7123e><i class="iconfont reco-account" data-v-1ff7123e><span data-v-1ff7123e>lzpeng723</span></i> <i class="iconfont reco-date" data-v-1ff7123e><span data-v-1ff7123e>10/21/2021</span></i> <!----> <i class="tags iconfont reco-tag" data-v-1ff7123e><span class="tag-item" data-v-1ff7123e>Kubernetes</span></i></div></div> <div class="theme-reco-content content__default"><p><a href="https://gitee.com/yooome/golang/blob/main/k8s%E8%AF%A6%E7%BB%86%E6%95%99%E7%A8%8B/Kubernetes%E8%AF%A6%E7%BB%86%E6%95%99%E7%A8%8B.md#2-kubernetes%E9%9B%86%E7%BE%A4%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA" target="_blank" rel="noopener noreferrer">原文链接<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> <a href="https://www.bilibili.com/video/BV1Qv41167ck?p=5" target="_blank" rel="noopener noreferrer">视频教程<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <h1 id="_2-kubernetes集群环境搭建"><a href="#_2-kubernetes集群环境搭建" class="header-anchor">#</a> 2. kubernetes集群环境搭建</h1> <h2 id="_2-1-前置知识点"><a href="#_2-1-前置知识点" class="header-anchor">#</a> 2.1 前置知识点</h2> <p>目前生产部署Kubernetes 集群主要有两种方式：</p> <p><strong>kubeadm</strong></p> <p>Kubeadm 是一个K8s 部署工具，提供<code>kubeadm init</code> 和<code>kubeadm join</code>，用于快速部署Kubernetes 集群。</p> <p>官方地址：<a href="https://kubernetes.io/docs/reference/setup-tools/kubeadm/kubeadm/" target="_blank" rel="noopener noreferrer">https://kubernetes.io/docs/reference/setup-tools/kubeadm/kubeadm/<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <p><strong>二进制包</strong></p> <p>从github 下载发行版的二进制包，手动部署每个组件，组成Kubernetes 集群。</p> <p>Kubeadm 降低部署门槛，但屏蔽了很多细节，遇到问题很难排查。如果想更容易可控，推荐使用二进制包部署Kubernetes 集群，虽然手动部署麻烦点，期间可以学习很多工作原理，也利于后期维护。</p> <p><img src="/assets/img/image-20200404094800622.cebaa87a.png" alt="image-20200404094800622"></p> <h2 id="_2-2-kubeadm-部署方式介绍"><a href="#_2-2-kubeadm-部署方式介绍" class="header-anchor">#</a> 2.2 kubeadm 部署方式介绍</h2> <p>kubeadm 是官方社区推出的一个用于快速部署kubernetes 集群的工具，这个工具能通过两条指令完成一个kubernetes 集群的部署：</p> <ul><li>创建一个Master 节点<code>kubeadm init</code></li> <li>将Node 节点加入到当前集群中<code>kubeadm join &lt;Master 节点的IP 和端口&gt;</code></li></ul> <h2 id="_2-3-安装要求"><a href="#_2-3-安装要求" class="header-anchor">#</a> 2.3 安装要求</h2> <p>在开始之前，部署Kubernetes 集群机器需要满足以下几个条件：</p> <ul><li>一台或多台机器，操作系统CentOS7.x-86_x64</li> <li>硬件配置：2GB 或更多RAM，2 个CPU 或更多CPU，硬盘30GB 或更多</li> <li>集群中所有机器之间网络互通</li> <li>可以访问外网，需要拉取镜像</li> <li>禁止swap 分区</li></ul> <h2 id="_2-4-最终目标"><a href="#_2-4-最终目标" class="header-anchor">#</a> 2.4 最终目标</h2> <ul><li>在所有节点上安装 Docker 和 kubeadm</li> <li>部署 Kubernetes Master</li> <li>部署容器网络插件</li> <li>部署 Kubernetes Node，将节点加入Kubernetes 集群中</li> <li>部署 Dashboard Web 页面，可视化查看Kubernetes 资源</li></ul> <h2 id="_2-5-准备环境"><a href="#_2-5-准备环境" class="header-anchor">#</a> 2.5 准备环境</h2> <p><img src="/assets/img/image-20210609000002940.7f42ba91.png" alt="image-20210609000002940"></p> <table><thead><tr><th style="text-align:left;">角色</th> <th style="text-align:left;">IP地址</th> <th style="text-align:left;">组件</th></tr></thead> <tbody><tr><td style="text-align:left;">CentOS7-Node1</td> <td style="text-align:left;">192.168.85.131</td> <td style="text-align:left;">docker，kubectl，kubeadm，kubelet</td></tr> <tr><td style="text-align:left;">CentOS7-Node2</td> <td style="text-align:left;">192.168.85.132</td> <td style="text-align:left;">docker，kubectl，kubeadm，kubelet</td></tr> <tr><td style="text-align:left;">CentOS7-Node3</td> <td style="text-align:left;">192.168.85.133</td> <td style="text-align:left;">docker，kubectl，kubeadm，kubelet</td></tr> <tr><td style="text-align:left;">CentOS7-Node4</td> <td style="text-align:left;">192.168.85.134</td> <td style="text-align:left;">docker，kubectl，kubeadm，kubelet</td></tr> <tr><td style="text-align:left;">CentOS7-Node5</td> <td style="text-align:left;">192.168.85.135</td> <td style="text-align:left;">docker，kubectl，kubeadm，kubelet</td></tr></tbody></table> <h2 id="_2-6-环境初始化"><a href="#_2-6-环境初始化" class="header-anchor">#</a> 2.6 环境初始化</h2> <h3 id="_2-6-1-检查操作系统的版本"><a href="#_2-6-1-检查操作系统的版本" class="header-anchor">#</a> 2.6.1 检查操作系统的版本</h3> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token comment"># 此方式下安装kubernetes集群要求Centos版本要在7.5或之上</span>
<span class="token function">cat</span> /etc/redhat-release
CentOS Linux release <span class="token number">7.9</span>.2009 <span class="token punctuation">(</span>Core<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="_2-6-2-主机名解析"><a href="#_2-6-2-主机名解析" class="header-anchor">#</a> 2.6.2 主机名解析</h3> <p>为了方便集群节点间的直接调用，在这个配置一下主机名解析，企业中推荐使用内部DNS服务器</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token comment"># 主机名成解析 编所使用台服务器的/etc/hosts文件，添加下面内容</span>
<span class="token number">192.168</span>.85.130 CentOS7-Node0
<span class="token number">192.168</span>.85.131 CentOS7-Node1
<span class="token number">192.168</span>.85.132 CentOS7-Node2
<span class="token number">192.168</span>.85.133 CentOS7-Node3
<span class="token number">192.168</span>.85.134 CentOS7-Node4
<span class="token number">192.168</span>.85.135 CentOS7-Node5
<span class="token number">192.168</span>.85.136 CentOS7-Node6
<span class="token number">192.168</span>.85.137 CentOS7-Node7
<span class="token number">192.168</span>.85.138 CentOS7-Node8
<span class="token number">192.168</span>.85.139 CentOS7-Node9
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><h3 id="_2-6-3-时间同步"><a href="#_2-6-3-时间同步" class="header-anchor">#</a> 2.6.3 时间同步</h3> <p>kubernetes要求集群中的节点时间必须精确一直，这里使用chronyd服务从网络同步时间</p> <p>企业中建议配置内部的时间同步服务器</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token comment"># 启动chronyd服务</span>
systemctl start chronyd
systemctl <span class="token builtin class-name">enable</span> chronyd
<span class="token function">date</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h3 id="_2-6-4-禁用-iptable-和-firewalld-服务"><a href="#_2-6-4-禁用-iptable-和-firewalld-服务" class="header-anchor">#</a> 2.6.4 禁用 iptable 和 firewalld 服务</h3> <p>kubernetes和docker 在运行的中会产生大量的iptables规则，为了不让系统规则跟它们混淆，直接关闭系统的规则</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token comment"># 1 关闭firewalld服务</span>
systemctl stop firewalld
systemctl disable firewalld
<span class="token comment"># 2 关闭iptables服务</span>
systemctl stop iptables
systemctl disable iptables
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h3 id="_2-6-5-禁用-selinux"><a href="#_2-6-5-禁用-selinux" class="header-anchor">#</a> 2.6.5 禁用 selinux</h3> <p>selinux是linux系统一下的一个安全服务，如果不关闭它，在安装集群中会产生各种各样的奇葩问题</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token comment"># 将 SELinux 设置为 permissive 模式（相当于将其禁用）</span>
<span class="token comment"># 临时设置</span>
<span class="token function">sudo</span> setenforce <span class="token number">0</span>
<span class="token comment"># 永久设置</span>
<span class="token comment"># 1 编辑 /etc/selinux/config 文件，修改SELINUX的值为permissive</span>
<span class="token assign-left variable">SELINUX</span><span class="token operator">=</span>permissive
<span class="token comment"># 2 使用 sed 命令</span>
<span class="token function">sed</span> -i <span class="token string">'s/^SELINUX=enforcing$/SELINUX=permissive/'</span> /etc/selinux/config
<span class="token comment"># 注意修改完毕之后需要重启linux服务</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h3 id="_2-6-6-禁用swap分区"><a href="#_2-6-6-禁用swap分区" class="header-anchor">#</a> 2.6.6 禁用swap分区</h3> <p>swap分区值的是虚拟内存分区，它的作用是物理内存使用完，之后将磁盘空间虚拟成内存来使用，启用swap设备会对系统的性能产生非常负面的影响，因此kubernetes要求每个节点都要禁用swap设备，但是如果因为某些原因确实不能关闭swap分区，就需要在集群安装过程中通过明确的参数进行配置说明</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token comment"># 编辑分区配置文件/etc/fstab，注释掉swap分区一行</span>
<span class="token comment"># 注意修改完毕之后需要重启linux服务</span>
<span class="token function">vim</span> /etc/fstab
<span class="token comment"># 注释掉 /dev/mapper/centos-swap swap</span>
<span class="token comment"># /dev/mapper/centos-swap swap</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="_2-6-7-修改linux的内核参数"><a href="#_2-6-7-修改linux的内核参数" class="header-anchor">#</a> 2.6.7 修改linux的内核参数</h3> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token comment"># 修改linux的内核采纳数，添加网桥过滤和地址转发功能</span>
<span class="token function">cat</span> <span class="token operator">&lt;&lt;</span><span class="token string">EOF<span class="token bash punctuation"> <span class="token operator">|</span> <span class="token function">sudo</span> <span class="token function">tee</span> /etc/sysctl.d/k8s.conf</span>
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF</span>
<span class="token comment"># 重新加载配置</span>
<span class="token function">sudo</span> sysctl -p
<span class="token comment"># 加载网桥过滤模块</span>
modprobe br_netfilter
<span class="token comment"># 查看网桥过滤模块是否加载成功</span>
lsmod <span class="token operator">|</span> <span class="token function">grep</span> br_netfilter
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><h3 id="_2-6-8-配置ipvs功能"><a href="#_2-6-8-配置ipvs功能" class="header-anchor">#</a> 2.6.8 配置ipvs功能</h3> <p>在Kubernetes中Service有两种带来模型，一种是基于iptables的，一种是基于ipvs的两者比较的话，ipvs的性能明显要高一些，但是如果要使用它，需要手动载入ipvs模块</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token comment"># 1.安装ipset和ipvsadm</span>
yum <span class="token function">install</span> ipset ipvsadm -y
<span class="token comment"># 2.添加需要加载的模块写入脚本文件</span>
<span class="token function">cat</span> <span class="token operator">&lt;&lt;</span><span class="token string">EOF<span class="token bash punctuation"> /etc/sysconfig/modules/ipvs.modules</span>
#!/bin/bash
modprobe br_netfilter
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack_ipv4
EOF</span>
<span class="token comment"># 3.为脚本添加执行权限</span>
<span class="token function">chmod</span> +x /etc/sysconfig/modules/ipvs.modules
<span class="token comment"># 4.执行脚本文件</span>
/bin/bash /etc/sysconfig/modeules/ipvs.modules
<span class="token comment"># 5.查看对应的模块是否加载成功</span>
lsmod <span class="token operator">|</span> <span class="token function">grep</span> -e -ip_vs -e nf_conntrack_ipv4
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><p>此时开始重启服务器</p> <h3 id="_2-6-9-安装docker"><a href="#_2-6-9-安装docker" class="header-anchor">#</a> 2.6.9 安装docker</h3> <p><s>1、切换镜像源</s> <s><code>wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d.docker-ce.repo</code></s></p> <p><s>2、查看当前镜像源中支持的docker版本</s> <s><code>yum list docker-ce --showduplicates</code></s></p> <p><s>3、安装特定版本的docker-ce</s> <s>必须制定--setopt=obsoletes=0，否则yum会自动安装更高版本</s> <s><code>yum install --setopt=obsoletes=0 docker-ce-18.06.3.ce-3.e17 -y</code></s></p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token comment"># 1、下载安装Docker</span>
<span class="token function">curl</span> -fsSL https://get.docker.com <span class="token operator">|</span> <span class="token function">bash</span> -s <span class="token function">docker</span> --mirror Aliyun
<span class="token comment"># 2、添加一个配置文件</span>
<span class="token comment">#Docker 在默认情况下使用Vgroup Driver为cgroupfs，而Kubernetes推荐使用systemd来替代cgroupfs</span>
<span class="token function">mkdir</span> /etc/docker
<span class="token comment"># 3、编辑Docker配置文件</span>
<span class="token function">cat</span> <span class="token operator">&lt;&lt;</span><span class="token string">EOF<span class="token bash punctuation"> /etc/docker/daemon.json</span>
{
    &quot;exec-opts&quot;: [&quot;native.cgroupdriver=systemd&quot;],
    &quot;data-root&quot;: &quot;/opt/data/docker&quot;,
    &quot;registry-mirrors&quot;:  [
        &quot;http://hub-mirror.c.163.com&quot;,
        &quot;https://docker.mirrors.ustc.edu.cn&quot;,
        &quot;https://registry.docker-cn.com&quot;,
        &quot;https://reg-mirror.qiniu.com&quot;,
        &quot;https://dockerhub.azk8s.cn&quot;
    ]
}
EOF</span>
<span class="token comment"># 5、启动dokcer</span>
systemctl restart <span class="token function">docker</span>
systemctl <span class="token builtin class-name">enable</span> <span class="token function">docker</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br></div></div><h3 id="_2-6-10-安装kubernetes组件"><a href="#_2-6-10-安装kubernetes组件" class="header-anchor">#</a> 2.6.10 安装Kubernetes组件</h3> <p><a href="https://developer.aliyun.com/mirror/kubernetes" target="_blank" rel="noopener noreferrer">参考链接<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token comment"># 1、由于kubernetes的镜像在国外，速度比较慢，这里切换成国内的镜像源</span>
<span class="token comment"># 2、编辑/etc/yum.repos.d/kubernetes.repo,添加下面的配置</span>
<span class="token punctuation">[</span>kubernetes<span class="token punctuation">]</span>       
<span class="token assign-left variable">name</span><span class="token operator">=</span>Kubernetes
<span class="token assign-left variable">baseurl</span><span class="token operator">=</span>https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
<span class="token assign-left variable">enabled</span><span class="token operator">=</span><span class="token number">1</span>
<span class="token assign-left variable">gpgcheck</span><span class="token operator">=</span><span class="token number">1</span>
<span class="token assign-left variable">repo_gpgcheck</span><span class="token operator">=</span><span class="token number">1</span>
<span class="token assign-left variable">gpgkey</span><span class="token operator">=</span>https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
<span class="token assign-left variable">exclude</span><span class="token operator">=</span>kubelet kubeadm kubectl

<span class="token comment"># 3、安装kubeadm、kubelet和kubectl</span>
yum <span class="token function">install</span> kubeadm kubelet kubectl -y

<span class="token comment"># 4、配置kubelet的cgroup</span>
<span class="token comment"># 编辑/etc/sysconfig/kubelet, 添加下面的配置</span>
<span class="token assign-left variable">KUBELET_CGROUP_ARGS</span><span class="token operator">=</span><span class="token string">&quot;--cgroup-driver=systemd&quot;</span>
<span class="token assign-left variable">KUBE_PROXY_MODE</span><span class="token operator">=</span><span class="token string">&quot;ipvs&quot;</span>

<span class="token comment"># 5、设置kubelet开机自启</span>
systemctl <span class="token builtin class-name">enable</span> kubelet
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><h3 id="_2-6-11-准备集群镜像"><a href="#_2-6-11-准备集群镜像" class="header-anchor">#</a> 2.6.11 准备集群镜像</h3> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token comment"># 在安装kubernetes集群之前，必须要提前准备好集群需要的镜像，所需镜像可以通过下面命令查看</span>
kubeadm config images list
<span class="token comment"># 拉取镜像</span>
kubeadm config images pull --image-repository<span class="token operator">=</span>registry.aliyuncs.com/google_containers
<span class="token comment"># 保存镜像</span>
<span class="token function">docker</span> save -o k8s-base-images.tar <span class="token variable"><span class="token variable">`</span><span class="token function">docker</span> images <span class="token operator">|</span> <span class="token function">grep</span> google_containers <span class="token operator">|</span> <span class="token function">awk</span> <span class="token string">'BEGIN{OFS=&quot;:&quot;;ORS=&quot; &quot;}{print $1,$2}'</span><span class="token variable">`</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h3 id="_2-6-11-集群初始化"><a href="#_2-6-11-集群初始化" class="header-anchor">#</a> 2.6.11 集群初始化</h3> <blockquote><p>下面的操作只需要在master节点上执行即可</p></blockquote> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token comment"># 创建集群</span>
kubeadm init --apiserver-advertise-address<span class="token operator">=</span><span class="token number">192.168</span>.85.131 --image-repository registry.aliyuncs.com/google_containers --service-cidr<span class="token operator">=</span><span class="token number">10.96</span>.0.0/12 --pod-network-cidr<span class="token operator">=</span><span class="token number">10.244</span>.0.0/16
<span class="token comment"># 创建必要文件</span>
<span class="token function">mkdir</span> -p <span class="token environment constant">$HOME</span>/.kube
<span class="token function">sudo</span> <span class="token function">cp</span> -i /etc/kubernetes/admin.conf <span class="token environment constant">$HOME</span>/.kube/config
<span class="token function">sudo</span> <span class="token function">chown</span> <span class="token variable"><span class="token variable">$(</span><span class="token function">id</span> -u<span class="token variable">)</span></span><span class="token builtin class-name">:</span><span class="token variable"><span class="token variable">$(</span><span class="token function">id</span> -g<span class="token variable">)</span></span> <span class="token environment constant">$HOME</span>/.kube/config
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><blockquote><p>下面的操作只需要在node节点上执行即可</p></blockquote> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>kubeadm <span class="token function">join</span> <span class="token number">192.168</span>.85.131:6443 --token awk15p.t6bamck54w69u4s8 <span class="token punctuation">\</span>
    --discovery-token-ca-cert-hash sha256:a94fa09562466d32d29523ab6cff122186f1127599fa4dcd5fa0152694f17117 
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>在master上查看节点信息</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>kubectl get nodes
NAME            STATUS     ROLES                  AGE   VERSION
centos7-node1   NotReady   control-plane,master   9h    v1.22.3
centos7-node3   NotReady   <span class="token operator">&lt;</span>none<span class="token operator">&gt;</span>                 9h    v1.22.3
centos7-node4   NotReady   <span class="token operator">&lt;</span>none<span class="token operator">&gt;</span>                 9h    v1.22.3
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="_2-6-13-安装网络插件-只在master节点操作即可"><a href="#_2-6-13-安装网络插件-只在master节点操作即可" class="header-anchor">#</a> 2.6.13 安装网络插件，只在master节点操作即可</h3> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token function">wget</span> https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>由于外网不好访问，如果出现无法访问的情况，可以直接用下面的 记得文件名是kube-flannel.yml，位置：/root/kube-flannel.yml内容：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>https://github.com/flannel-io/flannel/tree/master/Documentation/kube-flannel.yml
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><s>下载完成<code>kube-flannel.yml</code>后,将文件中的<code>quay.io</code>替换为<code>quay-mirror.qiniu.com</code></s></p> <p>下载完成<code>kube-flannel.yml</code>后,将文件中的<code>quay.io</code>替换为<code>quay.mirrors.ustc.edu.cn</code></p> <p>准备镜像</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token comment"># 拉取镜像</span>
<span class="token function">docker</span> pull rancher/mirrored-flannelcni-flannel-cni-plugin:v1.2
<span class="token function">docker</span> pull quay.mirrors.ustc.edu.cn/coreos/flannel:v0.15.0
<span class="token comment"># 保存镜像</span>
<span class="token function">docker</span> save -o k8s-network-flannel-images.tar <span class="token variable"><span class="token variable">`</span><span class="token function">docker</span> images <span class="token operator">|</span> <span class="token function">grep</span> flannel <span class="token operator">|</span> <span class="token function">awk</span> <span class="token string">'BEGIN{OFS=&quot;:&quot;;ORS=&quot; &quot;}{print $1,$2}'</span><span class="token variable">`</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="_2-6-14-重启kubelet和docker"><a href="#_2-6-14-重启kubelet和docker" class="header-anchor">#</a> 2.6.14 重启kubelet和docker</h3> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token comment"># 重启kubelet</span>
systemctl restart kubelet
<span class="token comment"># 重启docker</span>
systemctl restart <span class="token function">docker</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>使用配置文件启动fannel</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>kubectl apply -f kube-flannel.yml
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>等待它安装完毕 发现已经是 集群的状态已经是Ready</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>NAME            STATUS     ROLES                  AGE     VERSION
centos7-node1   Ready      control-plane,master   10m     v1.22.3
centos7-node2   Ready      <span class="token operator">&lt;</span>none<span class="token operator">&gt;</span>                 73s     v1.22.3
centos7-node3   Ready      <span class="token operator">&lt;</span>none<span class="token operator">&gt;</span>                 84s     v1.22.3
centos7-node4   Ready      <span class="token operator">&lt;</span>none<span class="token operator">&gt;</span>                 2m22s   v1.22.3
centos7-node5   Ready      <span class="token operator">&lt;</span>none<span class="token operator">&gt;</span>                 2m23s   v1.22.3
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>若没有变成Ready,可以检查 kube-system 的 pod 是否正常</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>kubectl get pods -n kube-system

NAME                                    READY   STATUS                  RESTARTS      AGE
coredns-7f6cbbb7b8-6lbzt                <span class="token number">0</span>/1     Pending                 <span class="token number">0</span>             9h
coredns-7f6cbbb7b8-flwg8                <span class="token number">0</span>/1     Pending                 <span class="token number">0</span>             9h
etcd-centos7-node1                      <span class="token number">1</span>/1     Running                 <span class="token number">1</span> <span class="token punctuation">(</span>9h ago<span class="token punctuation">)</span>    9h
kube-apiserver-centos7-node1            <span class="token number">1</span>/1     Running                 <span class="token number">1</span> <span class="token punctuation">(</span>9h ago<span class="token punctuation">)</span>    9h
kube-controller-manager-centos7-node1   <span class="token number">1</span>/1     Running                 <span class="token number">1</span> <span class="token punctuation">(</span>9h ago<span class="token punctuation">)</span>    9h
kube-flannel-ds-5nk4h                   <span class="token number">0</span>/1     Init:0/2                <span class="token number">0</span>             3m39s
kube-flannel-ds-5rf6h                   <span class="token number">0</span>/1     Init:0/2                <span class="token number">0</span>             3m39s
kube-flannel-ds-t6dv7                   <span class="token number">0</span>/1     Init:ImagePullBackOff   <span class="token number">0</span>             3m39s
kube-proxy-pwprt                        <span class="token number">1</span>/1     Running                 <span class="token number">1</span> <span class="token punctuation">(</span>9h ago<span class="token punctuation">)</span>    9h
kube-proxy-v68fz                        <span class="token number">1</span>/1     Running                 <span class="token number">1</span> <span class="token punctuation">(</span>31m ago<span class="token punctuation">)</span>   9h
kube-proxy-vbwb8                        <span class="token number">1</span>/1     Running                 <span class="token number">1</span> <span class="token punctuation">(</span>9h ago<span class="token punctuation">)</span>    9h
kube-scheduler-centos7-node1            <span class="token number">1</span>/1     Running                 <span class="token number">1</span> <span class="token punctuation">(</span>9h ago<span class="token punctuation">)</span>    9h

<span class="token comment"># 发现有镜像是 Init:ImagePullBackOff</span>
<span class="token comment"># 然后重新拉取镜像解决</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><h3 id="_2-6-16-kubeadm中的命令"><a href="#_2-6-16-kubeadm中的命令" class="header-anchor">#</a> 2.6.16 kubeadm中的命令</h3> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token comment"># 生成 新的token</span>
kubeadm token create --print-join-command
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h2 id="_2-7-集群测试"><a href="#_2-7-集群测试" class="header-anchor">#</a> 2.7 集群测试</h2> <h3 id="_2-7-1-创建一个nginx服务"><a href="#_2-7-1-创建一个nginx服务" class="header-anchor">#</a> 2.7.1 创建一个nginx服务</h3> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>kubectl create deployment nginx --image<span class="token operator">=</span>nginx
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="_2-7-2-暴露端口"><a href="#_2-7-2-暴露端口" class="header-anchor">#</a> 2.7.2 暴露端口</h3> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>kubectl expose deploy nginx --port<span class="token operator">=</span><span class="token number">80</span> --target-port<span class="token operator">=</span><span class="token number">80</span> --type<span class="token operator">=</span>NodePort
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="_2-7-3-查看服务"><a href="#_2-7-3-查看服务" class="header-anchor">#</a> 2.7.3 查看服务</h3> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>kubectl get pod,svc
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><div class="language-bash line-numbers-mode"><pre class="language-bash"><code>NAME                         READY   STATUS    RESTARTS   AGE
pod/nginx-6799fc88d8-qd2nm   <span class="token number">1</span>/1     Running   <span class="token number">0</span>          2m4s

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT<span class="token punctuation">(</span>S<span class="token punctuation">)</span>        AGE
service/kubernetes   ClusterIP   <span class="token number">10.96</span>.0.1    <span class="token operator">&lt;</span>none<span class="token operator">&gt;</span>        <span class="token number">443</span>/TCP        20m
service/nginx        NodePort    <span class="token number">10.99</span>.20.8   <span class="token operator">&lt;</span>none<span class="token operator">&gt;</span>        <span class="token number">80</span>:32200/TCP   113s
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>浏览器可以打开 http://192.168.85.131:32200 访问 nginx 服务</p></div></section> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">Last Updated: </span> <span class="time">1/28/2022, 8:09:41 AM</span></div></footer> <!----> <div class="comments-wrapper"><!----></div> <ul class="side-bar sub-sidebar-wrapper" style="width:12rem;" data-v-70334359><li class="level-2" data-v-70334359><a href="/blogs/k8s/k8s-cluster-build.html#_2-1-前置知识点" class="sidebar-link reco-side-_2-1-前置知识点" data-v-70334359>2.1 前置知识点</a></li><li class="level-2" data-v-70334359><a href="/blogs/k8s/k8s-cluster-build.html#_2-2-kubeadm-部署方式介绍" class="sidebar-link reco-side-_2-2-kubeadm-部署方式介绍" data-v-70334359>2.2 kubeadm 部署方式介绍</a></li><li class="level-2" data-v-70334359><a href="/blogs/k8s/k8s-cluster-build.html#_2-3-安装要求" class="sidebar-link reco-side-_2-3-安装要求" data-v-70334359>2.3 安装要求</a></li><li class="level-2" data-v-70334359><a href="/blogs/k8s/k8s-cluster-build.html#_2-4-最终目标" class="sidebar-link reco-side-_2-4-最终目标" data-v-70334359>2.4 最终目标</a></li><li class="level-2" data-v-70334359><a href="/blogs/k8s/k8s-cluster-build.html#_2-5-准备环境" class="sidebar-link reco-side-_2-5-准备环境" data-v-70334359>2.5 准备环境</a></li><li class="level-2" data-v-70334359><a href="/blogs/k8s/k8s-cluster-build.html#_2-6-环境初始化" class="sidebar-link reco-side-_2-6-环境初始化" data-v-70334359>2.6 环境初始化</a></li><li class="level-3" data-v-70334359><a href="/blogs/k8s/k8s-cluster-build.html#_2-6-1-检查操作系统的版本" class="sidebar-link reco-side-_2-6-1-检查操作系统的版本" data-v-70334359>2.6.1 检查操作系统的版本</a></li><li class="level-3" data-v-70334359><a href="/blogs/k8s/k8s-cluster-build.html#_2-6-2-主机名解析" class="sidebar-link reco-side-_2-6-2-主机名解析" data-v-70334359>2.6.2 主机名解析</a></li><li class="level-3" data-v-70334359><a href="/blogs/k8s/k8s-cluster-build.html#_2-6-3-时间同步" class="sidebar-link reco-side-_2-6-3-时间同步" data-v-70334359>2.6.3 时间同步</a></li><li class="level-3" data-v-70334359><a href="/blogs/k8s/k8s-cluster-build.html#_2-6-4-禁用-iptable-和-firewalld-服务" class="sidebar-link reco-side-_2-6-4-禁用-iptable-和-firewalld-服务" data-v-70334359>2.6.4 禁用 iptable 和 firewalld 服务</a></li><li class="level-3" data-v-70334359><a href="/blogs/k8s/k8s-cluster-build.html#_2-6-5-禁用-selinux" class="sidebar-link reco-side-_2-6-5-禁用-selinux" data-v-70334359>2.6.5 禁用 selinux</a></li><li class="level-3" data-v-70334359><a href="/blogs/k8s/k8s-cluster-build.html#_2-6-6-禁用swap分区" class="sidebar-link reco-side-_2-6-6-禁用swap分区" data-v-70334359>2.6.6 禁用swap分区</a></li><li class="level-3" data-v-70334359><a href="/blogs/k8s/k8s-cluster-build.html#_2-6-7-修改linux的内核参数" class="sidebar-link reco-side-_2-6-7-修改linux的内核参数" data-v-70334359>2.6.7 修改linux的内核参数</a></li><li class="level-3" data-v-70334359><a href="/blogs/k8s/k8s-cluster-build.html#_2-6-8-配置ipvs功能" class="sidebar-link reco-side-_2-6-8-配置ipvs功能" data-v-70334359>2.6.8 配置ipvs功能</a></li><li class="level-3" data-v-70334359><a href="/blogs/k8s/k8s-cluster-build.html#_2-6-9-安装docker" class="sidebar-link reco-side-_2-6-9-安装docker" data-v-70334359>2.6.9 安装docker</a></li><li class="level-3" data-v-70334359><a href="/blogs/k8s/k8s-cluster-build.html#_2-6-10-安装kubernetes组件" class="sidebar-link reco-side-_2-6-10-安装kubernetes组件" data-v-70334359>2.6.10 安装Kubernetes组件</a></li><li class="level-3" data-v-70334359><a href="/blogs/k8s/k8s-cluster-build.html#_2-6-11-准备集群镜像" class="sidebar-link reco-side-_2-6-11-准备集群镜像" data-v-70334359>2.6.11 准备集群镜像</a></li><li class="level-3" data-v-70334359><a href="/blogs/k8s/k8s-cluster-build.html#_2-6-11-集群初始化" class="sidebar-link reco-side-_2-6-11-集群初始化" data-v-70334359>2.6.11 集群初始化</a></li><li class="level-3" data-v-70334359><a href="/blogs/k8s/k8s-cluster-build.html#_2-6-13-安装网络插件-只在master节点操作即可" class="sidebar-link reco-side-_2-6-13-安装网络插件-只在master节点操作即可" data-v-70334359>2.6.13 安装网络插件，只在master节点操作即可</a></li><li class="level-3" data-v-70334359><a href="/blogs/k8s/k8s-cluster-build.html#_2-6-14-重启kubelet和docker" class="sidebar-link reco-side-_2-6-14-重启kubelet和docker" data-v-70334359>2.6.14 重启kubelet和docker</a></li><li class="level-3" data-v-70334359><a href="/blogs/k8s/k8s-cluster-build.html#_2-6-16-kubeadm中的命令" class="sidebar-link reco-side-_2-6-16-kubeadm中的命令" data-v-70334359>2.6.16 kubeadm中的命令</a></li><li class="level-2" data-v-70334359><a href="/blogs/k8s/k8s-cluster-build.html#_2-7-集群测试" class="sidebar-link reco-side-_2-7-集群测试" data-v-70334359>2.7 集群测试</a></li><li class="level-3" data-v-70334359><a href="/blogs/k8s/k8s-cluster-build.html#_2-7-1-创建一个nginx服务" class="sidebar-link reco-side-_2-7-1-创建一个nginx服务" data-v-70334359>2.7.1 创建一个nginx服务</a></li><li class="level-3" data-v-70334359><a href="/blogs/k8s/k8s-cluster-build.html#_2-7-2-暴露端口" class="sidebar-link reco-side-_2-7-2-暴露端口" data-v-70334359>2.7.2 暴露端口</a></li><li class="level-3" data-v-70334359><a href="/blogs/k8s/k8s-cluster-build.html#_2-7-3-查看服务" class="sidebar-link reco-side-_2-7-3-查看服务" data-v-70334359>2.7.3 查看服务</a></li></ul></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div><div class="reco-bgm-panel" data-v-b1d3339e><audio id="bgm" src="/music/1.m4a" data-v-b1d3339e></audio> <div class="reco-float-box" style="bottom:200px;z-index:999999;display:none;" data-v-b1d3339e data-v-41bcba48 data-v-b1d3339e><img src="https://y.qq.com/music/photo_new/T002R300x300M0000046Etze42qCxC_1.jpg" data-v-b1d3339e></div> <div class="reco-bgm-box" style="left:10px;bottom:10px;z-index:999999;" data-v-b1d3339e data-v-41bcba48 data-v-b1d3339e><div class="reco-bgm-cover" style="background-image:url(https://y.qq.com/music/photo_new/T002R300x300M0000046Etze42qCxC_1.jpg);" data-v-b1d3339e><div class="mini-operation" style="display:none;" data-v-b1d3339e><i class="reco-bgm reco-bgm-pause" style="display:none;" data-v-b1d3339e></i> <i class="reco-bgm reco-bgm-play" style="display:none;" data-v-b1d3339e></i></div> <div class="falut-message" style="display:none;" data-v-b1d3339e>
          播放失败
        </div></div> <div class="reco-bgm-info" data-v-b1d3339e data-v-41bcba48 data-v-b1d3339e><div class="info-box" data-v-b1d3339e><i class="reco-bgm reco-bgm-music music" data-v-b1d3339e></i>苍穹</div> <div class="info-box" data-v-b1d3339e><i class="reco-bgm reco-bgm-artist" data-v-b1d3339e></i>韩磊</div> <div class="reco-bgm-progress" data-v-b1d3339e><div class="progress-bar" data-v-b1d3339e><div class="bar" data-v-b1d3339e></div></div></div> <div class="reco-bgm-operation" data-v-b1d3339e><i class="reco-bgm reco-bgm-last last" data-v-b1d3339e></i> <i class="reco-bgm reco-bgm-pause pause" style="display:none;" data-v-b1d3339e></i> <i class="reco-bgm reco-bgm-play play" data-v-b1d3339e></i> <i class="reco-bgm reco-bgm-next next" data-v-b1d3339e></i> <i class="reco-bgm reco-bgm-volume1 volume" data-v-b1d3339e></i> <i class="reco-bgm reco-bgm-mute mute" style="display:none;" data-v-b1d3339e></i> <div class="volume-bar" data-v-b1d3339e><div class="bar" data-v-b1d3339e></div></div></div></div> <div class="reco-bgm-left-box" data-v-b1d3339e data-v-41bcba48 data-v-b1d3339e><i class="reco-bgm reco-bgm-left" data-v-b1d3339e></i></div></div></div><!----></div></div>
    <script src="/assets/js/app.b9ccfc55.js" defer></script><script src="/assets/js/4.8390fbcb.js" defer></script><script src="/assets/js/1.3334f995.js" defer></script><script src="/assets/js/28.57336847.js" defer></script>
  </body>
</html>
